<?php

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

// set timeout session
ini_set('session.gc-maxlifetime', 60*30);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';
JDEBUG ? $_PROFILER->mark('afterLoad') : null;
$app = JFactory::getApplication('administrator');

// load librarie B&G
include JPATH_BASE.'/includes/bgHelper.php';

// Initialise the application.
$app->initialise(array(
    'language' => $app->getUserState('application.lang')
));
/****** end thư viện joomla **/

$db = JFactory::getDbo();   


function getName($username, $db){
    $qr = "SELECT name FROM #__users WHERE username = '$username'";
    // echo $qr;
    $db->setQuery($qr);

    $result = $db->loadResult();
    return $result;
}


/**** update mese - column  MESE_USEM_NAME(nguoi gui)*/
/*
$query = "SELECT DISTINCT `MESE_USEM_CODE`,`MESE_USEM_NAME` FROM `mese`";
$db -> setQuery($query); 
$result = $db -> loadObjectList();

foreach($result as $re){
    $qr = "UPDATE mese SET MESE_USEM_NAME = '".getName($re->MESE_USEM_CODE, $db)."' WHERE MESE_USEM_CODE = '$re->MESE_USEM_CODE'";

    $db->setQuery($qr);
    if($db->query()){
        echo 'ok';
    }else{
        echo 'failed. <br />';
        echo $qr;
    }
}


$app = &JFactory::getApplication();
$app->close();
*/


/**** update mere - column  MERE_USEM_NAME(nguoi gui)*/

$query = "SELECT DISTINCT `MERE_USEM_CODE`,`MERE_USEM_NAME` FROM `mere`";
$db -> setQuery($query); 
$result = $db -> loadObjectList();

foreach($result as $re){
    $qr = "UPDATE mere SET MERE_USEM_NAME = '".getName($re->MERE_USEM_CODE, $db)."' WHERE MERE_USEM_CODE = '$re->MERE_USEM_CODE'";

    $db->setQuery($qr);
    if($db->query()){
        echo 'ok';
    }else{
        echo 'failed. <br />';
        echo $qr;
    }
}


$app = &JFactory::getApplication();
$app->close();
